package com.qdxht.dispenser.ui.activity.test;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.EditText;
import android.app.AlertDialog;
import android.view.View;
import android.text.TextUtils;

import com.qdxht.dispenser.base.BaseFragment;
import com.qdxht.dispenser.databinding.FragmentMainHomeBinding;
import com.qdxht.dispenser.interfaces.IRefresh;
import com.qdxht.dispenser.ui.activity.LocationTestActivity;
import com.qdxht.dispenser.ui.activity.MqttTestActivity;
import com.qdxht.dispenser.ui.activity.SerialPortTestActivity;
import com.qdxht.dispenser.ui.activity.main.SettingsActivity;
import com.qdxht.dispenser.ui.activity.TimerSwitchTestActivity;
import com.qdxht.dispenser.utils.BroadcastUtil;
import com.qdxht.dispenser.utils.ConfigUtil;
import com.hjq.toast.ToastUtils;
import com.qdxht.dispenser.utils.update.VersionUpdateUtil;

import timber.log.Timber;


/**
 * @author GJ
 * @Description: s
 * @date 2022/6/9 10:50
 */
public class HomeFragment extends BaseFragment<FragmentMainHomeBinding> implements IRefresh {

    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        return fragment;
    }

    @Override
    protected FragmentMainHomeBinding getBinding(LayoutInflater inflater, ViewGroup container) {
        binding = FragmentMainHomeBinding.inflate(inflater, container, false);
        return binding;
    }

    @Override
    protected void initView() {
        // 设置串口测试按钮点击事件
        binding.btnSerialPortTest.setOnClickListener(v -> {
            Timber.tag("SerialPortTest").d("启动串口测试页面");
            SerialPortTestActivity.start(requireContext());
        });

        // 设置MQTT测试按钮点击事件
        binding.btnMqttTest.setOnClickListener(v -> {
            Timber.tag("MqttTest").d("启动MQTT测试页面");
            MqttTestActivity.start(requireContext());
        });

        // 设置信号测试按钮点击事件
        binding.btnBanner.setOnClickListener(v -> {
            Timber.tag("SignalTest").d("启动信号测试页面");
//            SignalTestActivity.start(requireContext());
            SettingsActivity.start(requireContext());
        });

        // 设置定位测试按钮点击事件
        binding.btnLocationTest.setOnClickListener(v -> {
            Timber.tag("LocationTest").d("启动定位测试页面");
            startActivity(new Intent(requireContext(), LocationTestActivity.class));
        });

        // 设置定时开关机测试按钮点击事件
        binding.btnTimerSwitchTest.setOnClickListener(v -> {
            Timber.tag("TimerSwitchTest").d("启动定时开关机测试页面");
            TimerSwitchTestActivity.start(requireContext());
        });
        
        // 设置二维码设置按钮点击事件
        binding.btnQrcodeSettings.setOnClickListener(v -> {
            Timber.tag("QRCodeSettings").d("打开二维码设置弹框");
            showQRCodeSettingDialog();
        });

        // 安装测试
        binding.btnInstall.setOnClickListener(v -> {
            Timber.tag("QRCodeSettings").d("安装测试");
            if(!VersionUpdateUtil.isNewVersion(mActivity, "3.0.0")){
                ToastUtils.show("发现新版本，更新中..");
                BroadcastUtil.sendDownloadUpdateBroadcast(mActivity,
                        "https://oss.yidaihulian.cn/v3.0.0.apk");
            } else {
                ToastUtils.show("已经是最新版，无需更新");
            }
        });
    }
    
    /**
     * 显示二维码设置弹框
     */
    private void showQRCodeSettingDialog() {
        // 创建一个EditText用于输入二维码地址
        final EditText editText = new EditText(requireContext());
        editText.setHint("请输入二维码地址");
        
        // 获取当前二维码地址并设置到输入框
        String currentQRCode = ConfigUtil.getQRCode();
        if (!TextUtils.isEmpty(currentQRCode)) {
            editText.setText(currentQRCode);
            editText.setSelection(currentQRCode.length());
        }
        
        // 设置EditText的布局参数
        int padding = (int) (20 * getResources().getDisplayMetrics().density);
        editText.setPadding(padding, padding, padding, padding);
        
        // 创建AlertDialog
        new AlertDialog.Builder(requireContext())
            .setTitle("二维码设置")
            .setView(editText)
            .setPositiveButton("保存", (dialog, which) -> {
                String qrCode = editText.getText().toString().trim();
                if (TextUtils.isEmpty(qrCode)) {
                    ToastUtils.show("二维码地址不能为空");
                    return;
                }
                
                // 保存二维码地址
                ConfigUtil.setQRCode(qrCode);
                BroadcastUtil.sendQRCodeBroadcast(mActivity);
                ToastUtils.show("二维码地址保存成功");
                Timber.tag("QRCodeSettings").d("保存二维码地址: %s", qrCode);
            })
            .setNegativeButton("取消", null)
            .show();
    }

    @Override
    protected void initData() {

    }


    @Override
    public void onRefreshView() {

    }

}
